<template>
	<view class="page-main">
		<view class="cover">
			<view class="cover-title" v-if="vuex_user.status===0">
				<view style="font-size: 50rpx;color: #9a283a;">
					<text>等待与Ta绑定</text>
				</view>
				<view style="font-size: 36rpx;color: #503636;margin-top: 15px;">
					<text>赶快与另一半绑定，记录你们一起经历的种种甜蜜瞬间哦 ~</text>
				</view>
			</view>
			<view v-else class="cover-poetry">
				<view>
					世界上美好的东西不多，立秋傍晚从河对岸吹过来的风，和二十来岁笑起来要人命的你。
				</view>
				<view style="text-align: right;">
					——宋小君《下雨和见你》
				</view>

			</view>
		</view>

		<view>
			<u-button v-if="vuex_user.status===0" color="linear-gradient(#ffafde, #f51f9c)" shape="circle" open-type="share">
				立即邀请我的另一半
			</u-button>
			<u-button v-else color="linear-gradient(#ffb4b4, #ff0000)" shape="circle" @click="onUnbind()">
				解除与&ensp;{{parent.nickname}}&ensp;的绑定
			</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				parent: this.$tools.getPartner()
			}
		},
		onLoad() {

		},
		onShareAppMessage(res) {
			if (this.vuex_user) {
				const invite = {
					inviterId: this.vuex_user.id,
					deadline: new Date().getTime() + 30 * 60 * 1000,
				}
				const arr = [];
				for (let key in invite) {
					arr.push(`${key}=${invite[key]}`);
				}
				return {
					title: '对方邀请你成为伴侣',
					path: `/pages/index/index?${arr.join('&')}`,
					imageUrl: "/static/app-share-cover.jpg"
				}
			}
		},
		onShareTimeline(res) {
			return {
				title: '快来记录你们的甜蜜瞬间',
				path: '/pages/index/index'
			}
		},
		methods: {
			onUnbind() {
				uni.showModal({
					title: "提示",
					content: `解除情侣关系将清空与之所有信息，确认与${this.parent.nickName}解除情侣关系吗？`,
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '解除中',
								mask: true
							});
							let update = {
									partnerId: null,
									configId: null,
									integral: 0,
									state: '0'
								},
								userId = this.user._id,
								partnerId = this.user.partnerId,
								condition = `userId in ["${userId}","${partnerId}"]`

							const db = uniCloud.database()
							db.collection('user')
								.where(`_id in ["${userId}","${partnerId}"]`)
								.update(update).then((res) => {
									db.collection('partner_config')
										.doc(this.user.configId).remove()
									db.collection('tasklist')
										.where(condition).remove()
									db.collection('reward')
										.where(condition).remove()
									db.collection('exchange_rewards')
										.where(condition).remove()
									db.collection('anniversaries')
										.where(`partnerConfigId=="${this.user.configId}"`).remove()
									this.user.state = '0'
									
									uni.hideLoading();
									uni.$u.toast('解绑成功');
									this.$tools.setUser(update)
									this.$tools.setPartner(null)
									this.$tools.setPartnerConfig(null)
									uniCloud.callFunction({
										name: 'sendMail',
										data: {
											from: `甜蜜小屋<3482941896@qq.com>`,
											to: '1714607063@qq.com',
											subject: '解绑提醒',
											text: `${this.user.nickName}用户已与你解除绑定`
										}
									})
								}).catch((err) => {
									uni.$u.toast('解绑失败');
								})
						}
					}
				});
			},

		}
	}
</script>

<style>
	page {
		--bg-image: linear-gradient(#faacb9, #fabfd3);
	}

	.cover {
		margin: 20rpx 0;
		height: calc(100vh - 380rpx);
		border: 1px solid #b4b4b4;
		border-radius: 30px;
		background: url("https://vkceyugu.cdn.bspapp.com/VKCEYUGU-5d4c5487-5c0b-49a5-bdb6-e267774b4611/e067f395-c675-45bc-ba4c-67a4a3a76849.jpg") bottom repeat-y;
		background-size: 100%;
	}

	.cover-title {
		margin: 80rpx 70rpx;
		text-align: center;
	}

	.cover-poetry {
		margin: 80rpx 70rpx;
		font-size: 30rpx;
		color: #503636;
		line-height: 60rpx
	}
</style>
